home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5223 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.6 KB  |  133 lines

  1. Path: sun001.spd.dsccc.com!spd!jmccarty
  2. From: jmccarty@spd.dsccc.com (Mike McCarty)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to tell if a file exists in C
  5. Date: 9 Feb 1996 00:23:36 GMT
  6. Organization: DSC Communications Corporation, Plano, Texas USA
  7. Message-ID: <4fe468$h31@sun001.spd.dsccc.com>
  8. References: <823685019.AA00170@escan.demon.co.uk>
  9. NNTP-Posting-Host: aplo139.spd.dsccc.com
  10.  
  11. In article <823685019.AA00170@escan.demon.co.uk>,
  12. Bill Birrell  <bill@escan.demon.co.uk> wrote:
  13. ) > Hi, how do I find out if a file already exists
  14. ) > in UNIX C? On PCs I would do a findfirst/findnext,
  15. ) > is there an equivalent on Unix?
  16. )
  17. )    Check up on access() and stat(). There cannot be an exact equivalent to
  18. )Digital Research's findfirst and findnext functions in unix, because unix has
  19. )a completely different file structure from CP/M or PC-Dos (later MsDos), and
  20. )therefore has no idea what FCBs are. Files, directories and devices are all
  21. )accessed the same way, and when you become used to it, it is a *much* simpler
  22. )and more logical approach. Take a look at K&R Chapter 8 [Either edition - it's
  23. )in both].
  24. )
  25. )Bill.
  26.  
  27. The easiest way to determine whether a file is accessible is to open it
  28. with read access. If the open fails, then the file does not exist. If
  29. what you want to do is -search- for a file, then you need to call
  30. opendir(), readdir(), and closedir(). Look at the man pages
  31.  
  32. ---------------------------------------------------------------------
  33.  
  34. DIRECTORY(3)                         BSD                          DIRECTORY(3)
  35.  
  36.  
  37.  
  38. NAME
  39.      opendir, opendir_r, readdir, readdir_r, telldir, seekdir, rewinddir,
  40.      closedir - directory operations
  41.  
  42. SYNOPSIS
  43.      #include <sys/types.h>
  44.      #include <sys/dir.h>
  45.  
  46.      DIR *opendir(filename)
  47.      char *filename;
  48.  
  49.      int opendir_r (dir_name, dir_pointer)
  50.      const char *dir_name;
  51.      DIR *dir_pointer;
  52.  
  53.      struct direct *readdir(dirp)
  54.      DIR *dirp;
  55.  
  56.      int readdir_r (dir_pointer, result)
  57.      DIR *dir_pointer;
  58.      struct dirent *result;
  59.  
  60.      long telldir(dirp)
  61.      DIR *dirp;
  62.  
  63.      seekdir(dirp, loc)
  64.      DIR *dirp;
  65.      long loc;
  66.  
  67.      rewinddir(dirp)
  68.      DIR *dirp;
  69.  
  70.      closedir(dirp)
  71.      DIR *dirp;
  72.  
  73. DESCRIPTION
  74.      opendir opens the directory named by filename and associates a
  75.      "directory"stream with it.  opendir returns a pointer to be used to
  76.      identify the directory stream in subsequent operations.  The pointer NULL
  77.      is returned if filename cannot be accessed, or if it cannot malloc(3)
  78.      enough memory to hold the whole thing.
  79.  
  80.      readdir returns a pointer to the next directory entry.  It returns NULL
  81.      upon reaching the end of the directory or detecting an invalid seekdir
  82.      operation.
  83.  
  84.      telldir returns the current location associated with the named directory
  85.      stream.
  86.  
  87.      seekdir sets the position of the next readdir operation on the directory
  88.      stream.  The new position reverts to the one associated with the
  89.      directory stream when the telldir operation was performed.  Values
  90.      returned by telldir are good only for the lifetime of the DIR pointer
  91.      from which they are derived.  If the directory is closed and then
  92.      reopened, the telldir value may be invalidated due to undetected
  93.      directory compaction.  It is safe to use a previous telldir value
  94.      immediately after a call to opendir and before any calls to readdir.
  95.  
  96.      rewinddir resets the position of the named directory stream to the
  97.      beginning of the directory.
  98.  
  99.      closedir closes the named directory stream and frees the structure
  100.      associated with the DIR pointer.
  101.  
  102.      Sample code that searchs a directory for entry "name" is
  103.  
  104.           len = strlen(name);
  105.           dirp = opendir(".");
  106.           for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  107.                if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
  108.                     closedir(dirp);
  109.                     return FOUND;
  110.                }
  111.           closedir(dirp);
  112.           return NOT_FOUND;
  113.  
  114. NOTES
  115.      The opendir_r and readdir_r functions are the reentrant versions of the
  116.      opendir and readdir functions, respectively.  The opendir_r function
  117.      stores the new directory stream associated with dir_name at dir_pointer.
  118.      The readdir_r function stores the next directory entry at result.
  119.  
  120.      Upon successful completion, the opendir_r function returns 0 (zero).
  121.      Otherwise, -1 is returned.
  122.  
  123.      Upon successful completion, the readdir_r function returns 0 (zero).
  124.      Otherwise, -1 is returned.
  125.  
  126. SEE ALSO
  127.      open(2), close(2), read(2), lseek(2), dir(5)
  128.  
  129. ----
  130. char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
  131.  
  132. I don't speak for DSC.         <- They make me say that.
  133.